package com.manpower.utl;


import com.manpower.ui.R;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class LocationController {
	
	private static LocationController mInstance;
	private Context mContext;
	private LocationManager mLocationManager;
	private MyLocationListener mListener;
	private String mLat;
	private String mLon;
	
	public LocationController(Context context) {
		mContext = context;
	}

	public static LocationController getInstance(Context context) {
		if(mInstance == null) {
			mInstance = new LocationController(context);
		}
		return mInstance;
	}
	
	public void startLocate() {
		mLocationManager = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
		mListener = new MyLocationListener();
		mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 100000, mListener);
		Toast.makeText(mContext, R.string.start_locate, Toast.LENGTH_SHORT).show();
	}
	
	public void stopLocate() {
		if(mLocationManager != null) {
			mLocationManager.removeUpdates(mListener);
		}
	}
	
	private class MyLocationListener implements LocationListener {

		@Override
		public void onLocationChanged(Location arg0) {
			if(arg0 != null) {
				mLon = Double.toString(arg0.getLongitude());
				mLat = Double.toString(arg0.getLatitude());
				Toast.makeText(mContext, mContext.getResources().getString(R.string.lat_text) + mLat + ","
						+ mContext.getResources().getString(R.string.lon_text) + mLon, Toast.LENGTH_SHORT).show();
			}
			else {
				Log.d("loc", "err");
			}
		}

		@Override
		public void onProviderDisabled(String arg0) {
			// TODO Auto-generated method stub
			Toast.makeText(mContext, R.string.locate_off, Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onProviderEnabled(String arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
			// TODO Auto-generated method stub
			
		}
		
	}
}
